<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
import discountDialog from "../discountDialog/index.js";

export default {
  mixins: [
    Vue.prototype.$vFN.Mixins.Base,
    Vue.prototype.$vFN.Mixins.Edit,
    Vue.prototype.$vFN.Mixins.List,
  ],
  components: {
    SelectDataGoods: async () =>
      Vue.extend(await Vue.prototype.$vFN.Components.Base.SelectDataGoods()),
  },
  data() {
    return {
      appWindow: window,
      // 页面接口地址
      apiUrl: {
        list: "mall-site-portal/member-rights/listRuleProduct",
        delete: "mall-site-portal/member-rights/deleteRuleProduct",
        add: "mall-site-portal/member-rights/createRuleProduct",
      },
      // 表格数据删除展示结构
      tableDeleteConfig: {
        width: "400px",
        title: "确定删除吗?",
        columns: [
          {
            label: "商品名称",
            prop: "name",
            type: "",
          },
        ],
      },

      goodsIds: [], //添加的商品id的数组

      // 请求携带参数
      mixinList_tableSearchConst: {
        rightRuleId: "",
      },

      // 表格数据删除展示结构
      tableDeleteConfig: {
        width: "400px",
        title: "确定删除?",
        columns: [
          {
            label: "商品名称",
            prop: "productName",
            type: "",
          },
        ],
      },
    };
  },
  methods: {
    // 初始化
    async init() {
      this.mixinList_tableSearchConst.rightRuleId = this.rightRuleId; //从rule/home 传来
      // 请求第一页
      await this.mixinList_getTable();

      this.mixinList_tableData.forEach((item) => {
        this.goodsIds.push(item.productId);
      });

      console.log(this.goodsIds);

      // 显示
      this.mixinEdit_editVisible = true;
    },

    addGoods() {
      this.$vFN.Directives.SelectDataGoods({
        appWindow: this.appWindow,
        selectIds: this.goodsIds,
        count: -1,
        onSuccess: (res) => {
          console.error(res);
          this.goodsIds = res;

          this.addGoods2Rule();
        },
      });
    },

    // 打开修改框
    openUpdate(params_) {
      let idArr = [];
      if (params_.type === "single") {
        idArr.push(params_.id);
      } else if (params_.type === "batch") {
        this.mixinList_selectTableData.forEach((item) => {
          idArr.push(item.id);
        });
      }

      // 判断是否最少选择了一项
      if (idArr.length === 0) {
        this.$vFN.Tools.messageError("最少选择一项");
        return;
      }

      discountDialog.init({
        rightRuleId: this.rightRuleId, //从rule/home 传来
        type: params_.type, //类型,单个还是多个
        idArr, //当前数据数组
        onSuccess: (res) => {
          this.mixinList_getTable();
        },
      });
    },

    // 添加商品到规则中
    async addGoods2Rule() {
      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.add,
          data: {
            productIdLst: this.goodsIds,
            rightRuleId: this.rightRuleId, // 从rule/home 传来 权益id
          },
        },
        window
      );

      if (res._ok) {
        this.$vFN.Tools.messageSuccess(res.message);
        this.mixinList_refreshData();
      }
    },
  },
  mounted() {
    this.init();
  },
  activated() {},
};
</script>
